/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import jpa.exceptions.NonexistentEntityException;
import model.Obat;
import model.ObatQuery;
import model.HistoryOrder;
import model.HistoryOrderQuery;
import model.User;

  /**
   *
   * @author atikahSH
   *
   */

public class ObatManagementLogic {

    HttpServletRequest request;

    public ObatManagementLogic(HttpServletRequest request) {
        this.request = request;
    }

    public String tambahObat() throws ParseException {
        List<Obat> obats = new ArrayList<Obat>();
        Obat obat = new Obat();
        ObatQuery eq = new ObatQuery();
        HttpSession session = request.getSession();

        String namaObat = request.getParameter("namaObat");
        String bentukObat = request.getParameter("bentukObat");
        String deskripsi = request.getParameter("deskripsi");
        String tanggal = request.getParameter("tanggal");
        String ht = request.getParameter("hargaObat");
        User uname = (User) session.getAttribute("uname");
        if (namaObat != null) {
            obat.setIdUser(uname);
            obat.setNamaObat(namaObat);
            obat.setBentukObat(bentukObat);
            obat.setDeskripsi(deskripsi);
            obat.setTanggal(tanggal);
            session.setAttribute("tempAddObat", obat);
        } else {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahObat.jsp";
        }
        if (ht.equals("")) {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahObat.jsp";
        } else {
            try {
                int hargaObat = Integer.parseInt(ht);
                obat.setHargaObat(hargaObat);
            } catch (NumberFormatException nf) {
                session.setAttribute("error", "format harga obat harus angka");
                return "tambahObat.jsp";
            }
        }
        String kuantitas = request.getParameter("kuantitas");
        obat.setKuantitas(kuantitas);
        session.setAttribute("tempAddObat", obat);

        if (namaObat.equals("") || bentukObat.equals("") || deskripsi.equals("") || tanggal.equals("") || kuantitas.equals("")) {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahObat.jsp";
        } else {
            try {
                int rek = Integer.parseInt(kuantitas);
            } catch (NumberFormatException n) {
                session.setAttribute("error", "format kuantitas harus angka");
                return "tambahObat.jsp";
            }
            try {
                DateFormat formatter;
                Date date;
                formatter = new SimpleDateFormat("dd-MM-yyyy");
                date = (Date) formatter.parse(tanggal);
            } catch (ParseException e) {
                session.setAttribute("error", "tanggal salah, gunakan datepicker");
                return "tambahObat.jsp";
            }
            boolean hasilCheck = eq.checkObat(namaObat);
            if (!hasilCheck) {
                eq.createObat(obat);
                session.removeAttribute("tempAddObat");
                obats = eq.getMyObats(uname);
                request.setAttribute("obats", obats);
                return "daftarObat.jsp";
            } else {
                session.setAttribute("error", "nama obat sudah terpakai");
                return "tambahObat.jsp";
            }
        }
    }

    public String hapusObat() throws NonexistentEntityException {
        HttpSession session = request.getSession();
        String idObat = request.getParameter("idObat");
        List<Obat> obats = new ArrayList<Obat>();
        ObatQuery eq = new ObatQuery();

        if (session.getAttribute("uname") != null) {
            User uname = (User) session.getAttribute("uname");
            if (idObat == null) {
                obats = eq.getMyObats(uname);
                request.setAttribute("obats", obats);
            } else {
                int id = Integer.parseInt(idObat);
                Obat temp = eq.getObat(id);
                eq.deleteObat(id);
                obats = eq.getMyObats(uname);
                request.setAttribute("obats", obats);
            }
            return "daftarObat.jsp";
        } else {
            session.setAttribute("error", "sudah logout sebelumnya");
            obats = eq.getLimitedObats();
            request.setAttribute("obats", obats);
            session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Obats</a>");
            return "index.jsp";
        }
    }

    public String lihatObat() {
        HttpSession session = request.getSession();
        Obat obat = new Obat();
        List<Obat> obats = new ArrayList<Obat>();
        ObatQuery eq = new ObatQuery();
        if (session.getAttribute("uname") != null) {
            String temp = request.getParameter("idObat");
            int idObat = Integer.parseInt(temp);
            boolean hasilCheck = eq.checkObat(idObat);
            if (hasilCheck) {
                obat = eq.getObat(idObat);
                session.setAttribute("ename", obat);
                Obat ename = (Obat) session.getAttribute("ename");
                return "lihatObat.jsp";
            } else {
                session.setAttribute("error", "nama obat tidak ada");
                obats = eq.getLimitedObats();
                request.setAttribute("obats", obats);
                session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Obats</a>");
                return "index.jsp";
            }
        } else {
            String temp = request.getParameter("idObat");
            int idObat = Integer.parseInt(temp);
            boolean hasilCheck = eq.checkObat(idObat);
            if (hasilCheck) {
                obat = eq.getObat(idObat);
                session.setAttribute("ename", obat);
                Obat ename = (Obat) session.getAttribute("ename");
                return "lihatObatPublic.jsp";
            } else {
                session.setAttribute("error", "nama obat tidak ada");
                obats = eq.getLimitedObats();
                request.setAttribute("obats", obats);
                session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Obats</a>");
                return "index.jsp";
            }
        }
    }


    public String ubahObat() {
        HttpSession session = request.getSession();
        Obat obat = new Obat();
        List<Obat> obats = new ArrayList<Obat>();
        ObatQuery eq = new ObatQuery();

        if (session.getAttribute("uname") != null) {
            Obat ename = (Obat) session.getAttribute("ename");
            boolean hasilCheck = eq.checkObat(ename.getNamaObat());
            if (hasilCheck) {
                obat = ename;
                session.setAttribute("tempEditObat", obat);
                return "editObat.jsp";
            } else {
                session.setAttribute("error", "nama obat tidak ada");
                obats = eq.getLimitedObats();
                request.setAttribute("obats", obats);
                session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Obats</a>");
                return "index.jsp";
            }
        } else {
            session.setAttribute("error", "sudah logout sebelumnya");
            obats = eq.getLimitedObats();
            request.setAttribute("obats", obats);
            session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Obats</a>");
            return "index.jsp";
        }
    }


    public String simpanUbahObat() throws ParseException {
        HttpSession session = request.getSession();
        Obat obat = new Obat();
        List<Obat> obats = new ArrayList<Obat>();
        ObatQuery eq = new ObatQuery();

        if (session.getAttribute("uname") != null) {
            String namaObat = request.getParameter("namaObat");
            String bentukObat = request.getParameter("bentukObat");
            String deskripsi = request.getParameter("deskripsi");
            String tanggal = request.getParameter("tanggal");
            String ht = request.getParameter("hargaObat");
            obat = (Obat) session.getAttribute("ename");
            String tempEname = obat.getNamaObat();
            if (namaObat != null) {
                obat.setNamaObat(namaObat);
                obat.setBentukObat(bentukObat);
                obat.setDeskripsi(deskripsi);
                obat.setTanggal(tanggal);
                session.setAttribute("tempEditObat", obat);
            } else {
                session.setAttribute("error", "masih ada field kosong");
                return "editObat.jsp";
            }
            if (ht.equals("")) {
                session.setAttribute("error", "masih ada field kosong");
                return "editObat.jsp";
            } else {
                try {
                    int hargaObat = Integer.parseInt(ht);
                    obat.setHargaObat(hargaObat);
                } catch (NumberFormatException nf) {
                    session.setAttribute("error", "format harga obat harus angka");
                    return "editObat.jsp";
                }
            }
            String kuantitas = request.getParameter("kuantitas");
            obat.setKuantitas(kuantitas);
            session.setAttribute("tempEditObat", obat);

            if (namaObat.equals("") || bentukObat.equals("") || deskripsi.equals("") || tanggal.equals("") || ht.equals("")
                    || kuantitas.equals("")) {
                session.setAttribute("error", "masih ada field kosong");
                return "editObat.jsp";
            } else {
                try {
                    int rek = Integer.parseInt(kuantitas);
                } catch (NumberFormatException n) {
                    session.setAttribute("error", "format kuantitas harus angka");
                    return "editObat.jsp";
                }
                try {
                    DateFormat formatter;
                    Date date;
                    formatter = new SimpleDateFormat("dd-MM-yyyy");
                    date = (Date) formatter.parse(tanggal);
                } catch (ParseException e) {
                    session.setAttribute("error", "tanggal salah, gunakan datepicker");
                    return "editObat.jsp";
                }
                boolean hasilCheck = eq.checkObat(namaObat);
                if (!hasilCheck || namaObat.equals(tempEname)) {
                    session.setAttribute("ename", obat);
                    eq.editObat(obat);
                    session.removeAttribute("tempEditObat");
                    return "lihatObat.jsp";
                } else {
                    session.setAttribute("error", "username sudah terpakai");
                    return "editObat.jsp";
                }
            }
        } else {
            session.setAttribute("error", "sudah logout sebelumnya");
            obats = eq.getLimitedObats();
            request.setAttribute("obats", obats);
            session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Obats</a>");
            return "index.jsp";
        }
    }

    public String searchObats() {
        HttpSession session = request.getSession();
        List<Obat> obats = new ArrayList<Obat>();
        ObatQuery eq = new ObatQuery();
        String namaObat = request.getParameter("namaObat");
        if (namaObat != null && !namaObat.equals("")) {
            obats = eq.findObats(namaObat);
            request.setAttribute("obats", obats);
            session.setAttribute("show", "<a href='account?page=home'>Five Recent Obats</a>");
            return "cariObat.jsp";
        } else {
            session.setAttribute("show", "<a href='account?page=home'>Five Recent Obats</a>");
            return "cariObat.jsp";
        }
    }
}

